package logic

import (
	"context"
	"fmt"
	rand2 "math/rand"
	"shop/cache"
	"time"

	"shop/internal/svc"
	"shop/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *shop.SendSmsRequest) (*shop.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	result, _ := cache.Rdb.Get(l.ctx, "SendSmsErr"+in.Mobile).Result()
	if result != "" {
		return nil, fmt.Errorf("已超过五分钟请重新发送")
	}

	code := rand2.Intn(9000) + 1000
	//sms, err := pkg.SendSms(in.Mobile, strconv.Itoa(code))
	//if err != nil {
	//	return nil, err
	//}
	//if *sms.Body.Code == "OK" {
	//	return nil, fmt.Errorf(*sms.Body.Message)
	//}
	cache.Rdb.Set(l.ctx, "SendSms"+in.Mobile, code, 0)
	cache.Rdb.Expire(l.ctx, "SendSmsErr"+in.Mobile, time.Minute*5)
	cache.Rdb.Incr(l.ctx, "SendSmsErr"+in.Mobile)
	return &shop.SendSmsResponse{}, nil
}
